package dao;

import entity.Music;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LoveMusicDao {
    //添加音乐到喜欢列表
    public boolean insertLoveMusic(int userId,int musicId){
        Connection connection = null;
        PreparedStatement statement = null;
        connection = DBUtil.getConn();
        String sql = "insert into lovemusic(user_id,music_id) values(?,?)";
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            statement.setInt(2,musicId);
            int ret = statement.executeUpdate();
            if (ret == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.getClose(connection,statement,null);
        }
        return false;
    }
    //显示用户当前点赞过的音乐
    public List<Music> findLoveMusic(int user_id){
        List<Music> musicList = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        connection = DBUtil.getConn();
        String sql = "select m.id as m_id,title,singer,time,url,userid from lovemusic lm,music m " +
                "where m.id=lm.music_id and lm.user_id = ?";
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,user_id);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                Music music = new Music();
                music.setId(resultSet.getInt("m_id"));
                music.setTitle(resultSet.getString("title"));
                music.setSinger(resultSet.getString("singer"));
                music.setTime(resultSet.getString("time"));
                music.setUrl(resultSet.getString("url"));
                music.setUserid(resultSet.getInt("userid"));
                musicList.add(music);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.getClose(connection,statement,resultSet);
        }
        return musicList;
    }
    //添加喜欢的音乐，判断是否重复喜欢
    public boolean findLoveMusicByMusicIdAndUserId(int user_id,int musicId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        connection = DBUtil.getConn();
        String sql = "select * from lovemusic where user_id=? and musicId=?";
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,user_id);
            statement.setInt(2,musicId);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.getClose(connection,statement,null);
        }
        return false;
    }
    //根据关键字查询喜欢的歌单
    public List<Music> findLoveMusicByKeyAndUId(String str,int user_id){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Music> musicList = new ArrayList<>();
        connection = DBUtil.getConn();
        String sql = "select m.id as m_id,title,singer,time,url,userid from lovemusic lm,music m " +
                "where m.id=lm.music_id and lm.user_id = ? and title like ?";
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,user_id);
            str = "%"+str+"%";
            statement.setString(2,str);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Music music = new Music();
                music.setId(resultSet.getInt("m_id"));
                music.setTitle(resultSet.getString("title"));
                music.setSinger(resultSet.getString("singer"));
                music.setTime(resultSet.getString("time"));
                music.setUrl(resultSet.getString("url"));
                music.setUserid(resultSet.getInt("userid"));
                musicList.add(music);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.getClose(connection,statement,resultSet);
        }
        return musicList;
    }
    public int removeLoveMusic(int userId,int musicId){
        Connection connection = null;
        PreparedStatement statement = null;
        connection = DBUtil.getConn();
        String sql = "delete from lovemusic where user_id=? and music_id=?";
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            statement.setInt(2,musicId);
            int ret = statement.executeUpdate();
            if (ret == 1){
                return 1;//删除成功
            }
            return ret;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.getClose(connection,statement,null);
        }
        return 0;
    }
    public static void main(String[] args) {
        LoveMusicDao loveMusicDao = new LoveMusicDao();
        loveMusicDao.removeLoveMusic(1,2);
    }
    public static void main5(String[] args) {
        LoveMusicDao loveMusicDao = new LoveMusicDao();
        List<Music> musicList = loveMusicDao.findLoveMusicByKeyAndUId("素",1);
        for (Music music : musicList){
            System.out.println(music);
        }
    }
    public static void main2(String[] args) {
        LoveMusicDao loveMusicDao = new LoveMusicDao();
        List<Music> musicList = loveMusicDao.findLoveMusic((1));
        for (Music music : musicList){
            System.out.println(music);
        }
    }
    public static void main1(String[] args) {
        LoveMusicDao loveMusicDao = new LoveMusicDao();
        loveMusicDao.insertLoveMusic(1,2);
    }
}
